package com.lizard.schedule.widget;

import android.app.Activity;
import android.app.Dialog;
import android.content.Context;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;

import com.lizard.schedule.R;
import com.lizard.schedule.ui.view.timepicker.WheelMain;
import com.lizard.schedule.utils.DeviceUtil;
import com.lizard.schedule.utils.TimeUtil;

import java.util.Calendar;

/**
 * 日期时间选择器
 */
public class DateTimePickerDialog extends Dialog {

    private Builder mBuilder;

    private WheelMain mWheelMain;

    public DateTimePickerDialog(Context context) {
        super(context, R.style.ScheduleDialogTheme);

    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.dialog_datetime_picker);
        setCancelable(false);

        setDialogWidth();

        Button positiveBtn = (Button)findViewById(R.id.positive_btn);
        Button negativeBtn = (Button)findViewById(R.id.negative_btn);

        positiveBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                cancel();
                if(mBuilder.mPositiveButtonClick != null){
                    mBuilder.mPositiveButtonClick.onClick(view);
                    mBuilder.mPositiveButtonClick.onSelectTime(mWheelMain.getTime());
                }
            }
        });

        negativeBtn.setOnClickListener(new View.OnClickListener() {

            @Override
            public void onClick(View view) {
                cancel();
                if(mBuilder.mNegativeButtonClick != null){
                    mBuilder.mNegativeButtonClick.onClick(view);
                    mBuilder.mNegativeButtonClick.onSelectTime(mWheelMain.getTime());
                }
            }
        });

        View timepickerview= findViewById(R.id.timepicker);

        mWheelMain = new WheelMain(timepickerview, mBuilder.mShowDateWheel, mBuilder.mShowTimeWheel);
        mWheelMain.screenheight = DeviceUtil.getScreenHeight();

        Calendar calendar = Calendar.getInstance();
        if(mBuilder.mTime >= 0){
            calendar.setTimeInMillis(mBuilder.mTime);
        }

        int year = calendar.get(Calendar.YEAR);
        int month = calendar.get(Calendar.MONTH);
        int day = calendar.get(Calendar.DAY_OF_MONTH);
        int hour = calendar.get(Calendar.HOUR_OF_DAY);
        int min = calendar.get(Calendar.MINUTE);

        mWheelMain.initDateTimePicker(year, month, day, hour, min);

        // 现在时间
        TextView nowTime = (TextView) findViewById(R.id.now_time);
        nowTime.setText(getContext().getString(R.string.now_time) + TimeUtil.transferToDateTime3(System.currentTimeMillis()));
    }

    /**
     * 对话框宽度设置
     */
    private void setDialogWidth(){
        android.view.WindowManager.LayoutParams params = getWindow().getAttributes();
        params.width = DeviceUtil.getScreenWidth() * 9 / 10;
        getWindow().setAttributes(params);
    }

    public static class Builder{

        private Activity mActivity;

        private OnClickListener mPositiveButtonClick;

        private OnClickListener mNegativeButtonClick;

        private long mTime = -1;

        private boolean mShowDateWheel = true;

        private boolean mShowTimeWheel = true;

        public Builder(Activity activity){
            mActivity = activity;
        }

        public Builder setNegativeButton(OnClickListener onClickListener){
            mNegativeButtonClick = onClickListener;
            return this;
        }

        public Builder setPositiveButton(OnClickListener onClickListener){
            mPositiveButtonClick = onClickListener;
            return this;
        }

        public Builder setTime(long time){
            mTime = time;
            return this;
        }

        public Builder setShowDateWheel(boolean show){
            mShowDateWheel = show;
            return this;
        }

        public Builder setShowTimeWheel(boolean show){
            mShowTimeWheel = show;
            return this;
        }

        /**
         * 显示对话框
         */
        public void show(){
            DateTimePickerDialog dateTimePickerDialog = new DateTimePickerDialog(mActivity);
            dateTimePickerDialog.mBuilder = this;
            dateTimePickerDialog.show();
        }

    }

    public interface OnClickListener extends View.OnClickListener{

        /**
         * 格式: yyyy-M-d HH:mm
         *
         * @param time
         */
        public void onSelectTime(String time);

    }
}
